/**
 * 
 */
package service.xml;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;

import org.apache.commons.lang.StringUtils;
import org.exolab.castor.mapping.Mapping;
import org.exolab.castor.mapping.MappingException;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.Unmarshaller;
import org.exolab.castor.xml.ValidationException;
import org.xml.sax.InputSource;

import app.constants.CollabVizConstants;
import app.exceptions.CollabVizException;

/**
 * @author poornima
 * 
 */
public class XMLService {

	private Mapping mappingConfig;

	public XMLService(String mappingFilePath) throws CollabVizException {
		loadMappingConfig(mappingFilePath);
	}

	private void loadMappingConfig(String filePath) throws CollabVizException {
		if (filePath == null
				|| org.apache.commons.lang.StringUtils.isEmpty(filePath)) {
			throw new CollabVizException(
					CollabVizConstants.XML_CONFIG_FILE_ERROR);
		}
		mappingConfig = new Mapping();
		try {
			mappingConfig.loadMapping(filePath);
		} catch (IOException e) {
			e.printStackTrace();
			throw new CollabVizException(CollabVizConstants.FILE_IO_ERROR);
		} catch (MappingException e) {
			e.printStackTrace();
			throw new CollabVizException(
					CollabVizConstants.XML_MAPPING_FILE_ERROR);
		}
	}

	public Object unmarshall(String filePath) throws CollabVizException {
		if (filePath == null
				|| org.apache.commons.lang.StringUtils.isEmpty(filePath)) {
			throw new CollabVizException(
					CollabVizConstants.XML_CONFIG_FILE_ERROR);
		}
		Object result = null;
		try {
			Unmarshaller unmarshal = new Unmarshaller(mappingConfig);
			result = unmarshal.unmarshal(new InputSource(new FileReader(
					filePath)));
		} catch (MappingException e) {
			e.printStackTrace();
			throw new CollabVizException(
					CollabVizConstants.FILE_UNMARSHALL_ERROR);
		} catch (MarshalException e) {
			e.printStackTrace();
			throw new CollabVizException(CollabVizConstants.MARSHALL_ERROR);
		} catch (ValidationException e) {
			e.printStackTrace();
			throw new CollabVizException(
					CollabVizConstants.FILE_VALIDATION_ERROR);
		} catch (FileNotFoundException e) {
			System.out.println("Warning: file not found: " + filePath);

		}
		return result;
	}

	public void marshall(String filePath, Object obj) throws CollabVizException {
		if (filePath == null || StringUtils.isEmpty(filePath) || obj == null) {
			throw new CollabVizException(
					CollabVizConstants.FILE_MARSHALL_INPUT_ERROR);
		}
		try {
			Marshaller marshall = new Marshaller(new OutputStreamWriter(
					new FileOutputStream(filePath)));
			marshall.setMapping(mappingConfig);
			marshall.marshal(obj);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			throw new CollabVizException(
					CollabVizConstants.FILE_NOT_FOUND_ERROR);
		} catch (IOException e) {
			e.printStackTrace();
			throw new CollabVizException(CollabVizConstants.FILE_IO_ERROR);
		} catch (MappingException e) {
			e.printStackTrace();
			throw new CollabVizException(CollabVizConstants.MAPPING_FILE_ERROR);
		} catch (MarshalException e) {
			e.printStackTrace();
			throw new CollabVizException(CollabVizConstants.MARSHALL_ERROR);
		} catch (ValidationException e) {
			e.printStackTrace();
			throw new CollabVizException(
					CollabVizConstants.FILE_VALIDATION_ERROR);
		}
	}
}
